<template>
  <el-dialog
    :title="!dataForm.spellId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="商品id" prop="shopId">
      <el-input v-model="dataForm.shopId" placeholder="商品id"></el-input>
    </el-form-item>
    <el-form-item label="用户id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="团长名称" prop="leaderName">
      <el-input v-model="dataForm.leaderName" placeholder="团长名称"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="修改时间"></el-input>
    </el-form-item>
    <el-form-item label="几人团" prop="people">
      <el-input v-model="dataForm.people" placeholder="几人团"></el-input>
    </el-form-item>
    <el-form-item label="几人参与" prop="peopleCount">
      <el-input v-model="dataForm.peopleCount" placeholder="几人参与"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          spellId: 0,
          shopId: '',
          userId: '',
          leaderName: '',
          createTime: '',
          updateTime: '',
          people: '',
          peopleCount: ''
        },
        dataRule: {
          shopId: [
            { required: true, message: '商品id不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          leaderName: [
            { required: true, message: '团长名称不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ],
          people: [
            { required: true, message: '几人团不能为空', trigger: 'blur' }
          ],
          peopleCount: [
            { required: true, message: '几人参与不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.spellId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.spellId) {
            this.$http({
              url: this.$http.adornUrl(`/consumption/spellrecord/info/${this.dataForm.spellId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.shopId = data.spellRecord.shopId
                this.dataForm.userId = data.spellRecord.userId
                this.dataForm.leaderName = data.spellRecord.leaderName
                this.dataForm.createTime = data.spellRecord.createTime
                this.dataForm.updateTime = data.spellRecord.updateTime
                this.dataForm.people = data.spellRecord.people
                this.dataForm.peopleCount = data.spellRecord.peopleCount
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/consumption/spellrecord/${!this.dataForm.spellId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'spellId': this.dataForm.spellId || undefined,
                'shopId': this.dataForm.shopId,
                'userId': this.dataForm.userId,
                'leaderName': this.dataForm.leaderName,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'people': this.dataForm.people,
                'peopleCount': this.dataForm.peopleCount
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
